/**
 * 项目名：	Rumba Plan
 * 
 * 版权所有(C)，上海海鼎信息工程股份有限公司，2006，所有权利保留。
 * 
 * 文件名：	HFieldSelect.java
 * 模块说明：	Select输入框 | 服务端界面控件
 * 修改历史：
 * 2006-5-26 - lxm - 创建。
 */
package com.hd123.rumba.jsp;

import java.math.BigDecimal;

import org.apache.ecs.html.Option;
import org.apache.ecs.html.Select;

import com.hd123.rumba.request.Dictionary;
import com.hd123.rumba.util.AccessMode;

/**
 * Select输入框 | 服务端界面控件
 * <p>
 * 包括ComboBox和ListBox（单选）两种界面效果。
 * <p>
 * 
 * 页面模型：<br>
 * <table border=0 cellspacing=3 cellpadding=0>
 * <tr bgcolor=skyblue>
 * <td>界面元素</td>
 * <td>对象属性</td>
 * </tr>
 * <tr>
 * <td><code>&lt;td&gt;</code></td>
 * <td><i>tdLabel</i></td>
 * </tr>
 * <tr bgcolor=aliceblue>
 * <td><code>&nbsp;&nbsp;...</code></td>
 * <td>参见{@link LabelHField LabelHField}</td>
 * </tr>
 * <tr>
 * <td><code>&lt;td&gt;</code></td>
 * <td><i>tdContent</i></td>
 * </tr>
 * <tr bgcolor=aliceblue>
 * <td><code>&nbsp;&nbsp;&lt;select&gt;</code></td>
 * <td>select</td>
 * </tr>
 * </table>
 * 
 * @author lxm
 * 
 */
public class HFieldSelect extends OptionalHField {

  public HFieldSelect(Dictionary dd, String name, String caption, Object value, Object[] params) {
    super(dd, name, caption, value, params);
  }

  public HFieldSelect(Dictionary dd, String name, String caption, Object value) {
    super(dd, name, caption, value);
  }

  private Integer selectStyle = new Integer(0);
  private Select select = null;

  /** selectStyle取值：ComboBox */
  public static final Integer VALUE_SELECTSTYLE_COMBOBOX = new Integer(0);
  /** selectStyle取值：ListBox */
  public static final Integer VALUE_SELECTSTYLE_LISTBOX = new Integer(1);

  /**
   * 属性：风格。可能的取值参见"VALUE_SELECTSTYLE_"开头的常量。默认为VALUE_SELECTSTYLE_COMBOBOX。<br>
   * 对应的参数名"selectStyle"，下一参数类型为Integer。
   */
  public Integer getSelectStyle() {
    return selectStyle;
  }

  public void setSelectStyle(Integer selectStyle) {
    this.selectStyle = selectStyle;
  }

  @Override
  protected boolean readParam(String paramName, Object paramValue) {
    if (super.readParam(paramName, paramValue))
      return true;
    if ("selectStyle".equals(paramName)) {
      this.selectStyle = (Integer) paramValue;
      return true;
    }
    return false;
  }
  
  /**
   * 只读属性：存放输入部分的界面元素&lt;select&gt;<br>
   * 对应的参数名"select"。
   */
  public Select getSelect() {
    if (select == null) {
      select = new Select();
      select.setID(this.getName());
      select.setName(this.getName());
    }
    return select;
  }

  protected void buildHTML(int accessMode, int promptMode) {
    super.buildHTML(accessMode, promptMode);
    Select select = this.getSelect();
    String className = "small";
    if (VALUE_SELECTSTYLE_LISTBOX.equals(selectStyle))
      className = className + "_list";
    else
      className = className + "_combo";
    if (accessMode != AccessMode.READWRITE) {
      select.setDisabled(true);
      className = className + "_static";
    }
    select.setClass(className);

    if (accessMode != AccessMode.INVISIBLE) {
      KeyValueList options = this.getOptions();
      for (int i = 0; i < options.size(); i++) {
        Object value = options.getKey(i);
        String valueCaption = (String) options.get(value);
        Option opt = new Option();
        if (value instanceof String)
          opt.setValue((String) value);
        else if (value instanceof Integer)
          opt.setValue(((Integer) value).intValue());
        else if (value instanceof Double)
          opt.setValue(((Double) value).doubleValue());
        else if (value instanceof BigDecimal)
          opt.setValue(((BigDecimal) value).doubleValue());
        else
          opt.setValue(value.toString());
        opt.addElement(valueCaption);
        if (value.equals(this.getValue()))
          opt.setSelected(true);
        select.addElement(opt);
      }
    }

    this.assignElementAttrs(select, "select");
    this.getTdContent().addElement(select);
  }
}
